package com.itheima.proxy.cglib;

import com.itheima.proxy.jdk.TargetInterface;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import javax.swing.text.html.HTML;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author wuli
 * @create 2021-11-28 10:55
 */
public class ProxyTest {

    public static void main(String[] args) {

        final Target target = new Target();

        final Advice advice = new Advice();

        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Target.class);
        enhancer.setCallback(new MethodInterceptor() {
            public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
                advice.before();
                Object obj = method.invoke(target, args);
                advice.afterReturning();
                return obj;
            }
        });
        Target proxy = (Target) enhancer.create();
        proxy.save();

    }
}
